package cn.tedu.review;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Iterator;

/**本类用于回顾集合*/
public class TestCollection {
    public static void main(String[] args) {
        Collection<Integer> a = new ArrayList();
        a.add(100);
        a.add(200);
        a.add(300);
        a.add(400);
        a.add(500);
        System.out.println(a);
        a.clear();
        //这里是顶级父类Object的方法
        System.out.println(a.hashCode());
        System.out.println(a.equals(100));
        System.out.println(a);

        //测试Coiiection自身方法
        System.out.println(a.contains(600));
        System.out.println(a.isEmpty());
        System.out.println(a.remove(100));
        System.out.println(a);
        System.out.println(a.size());
        System.out.println(Arrays.toString(a.toArray()));//集合转数组，并打印

        //测试集合间的方法
        Collection<Integer> c2 = new ArrayList();
        c2.add(2);
        c2.add(4);
        c2.add(6);
        System.out.println(c2);
        a.addAll(c2);
        System.out.println(a.containsAll(c2));
        System.out.println(a.removeAll(c2));
        System.out.println(a.retainAll(c2));
        System.out.println(a.size());
        System.out.println(a);

        //使用迭代器迭代集合
        Iterator<Integer> it = c2.iterator();//获取迭代器
        while (it.hasNext()){
            System.out.println(it.next());
        }
    }
}
